﻿using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

///<summary>
///Class chua phuong thuc ket noi lam viec voi tang bussiness
///(business tier)->receives requests from the presentation tier 
///and returns a result to the presentation tier depending on the business logic it contains
///</summary>
namespace smartphone.App_Code
{
    public static class GenericDataAccess
    {
        //ham khoi tao static
        static GenericDataAccess()
        {

        }

        //thuc thi command va tra ve mot doi tuong DataTable
        public static DataTable ExecuteSelectCommand(DbCommand command)
        {
            //table tra ve
            DataTable table;

            try
            {
                command.Connection.Open();
                DbDataReader reader = command.ExecuteReader();
                table = new DataTable();
                table.Load(reader);
            }
            catch (Exception ex)
            {
                Utilities.LogError(ex);
                throw;
            }
            finally
            {
                command.Connection.Close();
            }

            return table;
        }

        //ham tao doi tuong DbCommand qua phuong thuc ao CreateCommand cua lop DbProviderFactory 
        public static DbCommand CreateCommand()
        {
            //lay dataprovider name tu ESmartConfig
            string dataProviderName = ESmartConfig.DbProviderName;
            //lay connectionString name tu ESmartConfig
            string connectionString = ESmartConfig.DbConnectionString;
            // Create a new data provider factory
            DbProviderFactory factory = DbProviderFactories.GetFactory(dataProviderName);
            // Obtain a database specific connection object
            DbConnection conn = factory.CreateConnection();
            // Set the connection string
            conn.ConnectionString = connectionString;
            // Create a database specific command object
            DbCommand comm = conn.CreateCommand();
            // Set the command type to stored procedure
            comm.CommandType = CommandType.StoredProcedure;
            // Return the initialized command object
            return comm;
        }

        /// <summary>
        /// Thuc hien ham exexuteNonQueyr va tra ve do dong bi tac dong
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public static int ExecuteNonQuery(DbCommand command)
        {
            //so dong bi tac dong duoc tra ve
            int affectedRows = -1;

            try
            {
                command.Connection.Open();
                int a = command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Utilities.LogError(ex);
                throw;
            }
            finally
            {
                command.Connection.Close();
            }

            return affectedRows;
        }


        /// <summary>
        /// Thuc hien ham ExecuteScalar va tra ve 1 dong dau tien bi tac dong duoi dang 1 chuoi
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public static string ExecuteScalar(DbCommand command)
        {
            //chuoi chua gia tri tra ve
            string returnValue = "";
            try
            {
                command.Connection.Open();
                returnValue = command.ExecuteScalar().ToString();
            }
            catch (Exception ex)
            {
                Utilities.LogError(ex);
                throw;
            }
            finally
            {
                command.Connection.Close();
            }

            return returnValue;
        }
    }
}